package com.akk.platform.oem.business;

import com.akk.module.avs.entity.AvsYbLoanAccess;
import com.akk.module.avs.enums.AccessCardEnums;
import com.akk.module.avs.service.AvsYbLoanAccessService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.Map;

@Component
public class YbLoanAccessBusiness {

    @Autowired
    private AvsYbLoanAccessService loanAccessService;

    public RestResponse list(Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),"");
        PageHelper.startPage(query.getPage(),query.getLimit());
        Map params = query.getSearch();
        params.put("appId", ShiroUtils.getAdmin().getAppId());
        List<AvsYbLoanAccess> list = loanAccessService.findByParams(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    public RestResponse handel(Long accesId){
        AvsYbLoanAccess loanAccess = loanAccessService.findById(accesId);
        if (loanAccess == null)
            return RestResponse.build("1","信息不存在",null);
        if (loanAccess.getStatus() == AccessCardEnums.AccessCardStatus.PROCESSED.getValue())
            return RestResponse.build("1","此条信息已经处理","");
        loanAccess.setStatus(AccessCardEnums.AccessCardStatus.PROCESSED.getValue());
        loanAccessService.update(loanAccess);
        return RestResponse.build("success");
    }

    public RestResponse info(Long accessId) {
        return RestResponse.build(loanAccessService.findById(accessId));
    }
}
